class TemplateController < ApplicationController

def get_theme_root

  current_directory=Dir.getwd;
  current_directory=current_directory.concat("/themes");
  return current_directory;
       
end

def get_all_themes

  $theme_array=Array.new();
  i=0;
  j=0;
  k=0;
  subdirectory_count=0;

  $theme_root=get_theme_root();
  themes_dir=Dir.new($theme_root);

  for theme_name in themes_dir
    if(!theme_name.index("."))
      $theme_array[i]=theme_name;
      i=i+1;
    end
  end

  # To get the no of themes
  theme_array_count=$theme_array.length; 

  # Taking the subdirectory count under themes
  
  for j in 0..theme_array_count-1
    subdir_path=$theme_root + "/" + $theme_array[j];
    subdir=Dir.new(subdir_path);
    for subdir_name in subdir
      if(!subdir_name.index(".") and subdir_name!= "images")           
        subdirectory_count=subdirectory_count+1;
      end
    end
  end

  # Storing an associative array for themes and its subdirectories

  $subdirectory_files=Array.new(subdirectory_count) { Hash.new };
  
  for j in 0..theme_array_count-1
    subdir_path=$theme_root + "/" + $theme_array[j];
    subdir=Dir.new(subdir_path);
    for subdir_name in subdir
      if(!subdir_name.index(".") and subdir_name!= "images")           
        $subdirectory_files[k][$theme_array[j]] = subdir_name; 
        k=k+1;
      end
    end  
  end
      
end


def get_subdirectories
  
  $subdirectory_link="";
  $current_theme=params[:list]; 
  $temp_subdir=Array.new();
  k=0;
  get_all_themes();

  subdirectory_array_count=$subdirectory_files.length;  

  # Taking the subdirectories under the chosen theme from the option box
  
  for index in 0..subdirectory_array_count-1 
    if($subdirectory_files[index][$current_theme]!= nil)
        $temp_subdir[k]=$subdirectory_files[index][$current_theme];
         $subdirectory_link=$subdirectory_link+"<%=link_to_remote '"+$subdirectory_files[index][$current_theme]+"', :update => \"filenames\", :url => { :action => \"get_files\", :id => '"+$subdirectory_files[index][$current_theme]+"'} %>"+"<br>";
         k=k+1;
    end
  end
  
  temp_arr_count=$temp_subdir.length;
  render :inline => $subdirectory_link;
  
end


def get_files
  
  $clicked_link=params[:id];
  $file_names=Array.new();
  $file_names_link="";
  j=0;
  temp_subdir_count=$temp_subdir.length;
  
  # Taking all the files based on the selected subdirectory
  
  for index in 0..temp_subdir_count-1
    if($clicked_link == $temp_subdir[index])
       file_path=$theme_root + "/" + $current_theme + "/" + $temp_subdir[index];        
       file_dir=Dir.new(file_path);
         
        for file_name in file_dir
          if(!file_name.match(".svn"))
            $file_names[j]=file_name;
            $file_names_link=$file_names_link+"<%=link_to_remote '"+file_name+"' , :update=>\"body\", :url => { :action => \"get_file_data\", :id => '"+file_name+"'} %>"+"<br>";
            j=j+1;
          end
        end 
    end
  end

  render :inline => $file_names_link;
end


def get_file_data

  $clicked_file=params[:id];
  $file_data="";
  
  file_name_array_length=$file_names.length;

  # Taking the file content based on the chosen file
  
    for index in 0..file_name_array_length
      if($clicked_file == $file_names[index])
        file_path=$theme_root + "/" + $current_theme + "/" + $clicked_link + "/" + $clicked_file;                
          File.open(file_path,"r") do |file|
            file.each_line { |data| 
            $file_data = $file_data + data;
            }
          end 
      end
    end

    render :inline => $file_data;
end

end